<h2>
	<?php 
		if (!empty($_lang) && $_lang != @DEFAULT_LANGUAGE) {
			if ($flag_prefix = Configure::read('LilcartPlugin.l18nFlagUrl')) {
				echo $html->image($flag_prefix.$_lang.'.png', array('style'=>'height:36px'));
			} else {
				echo $sanitize->html($_lang);
			}
			echo ' ';
		}
		__d('lilcart', 'Add / Edit Item');
	?>
</h2>
<div class="form">
	<?php echo $javascript->link('jquery-1.2.6.min', false).PHP_EOL; ?>
	<?php echo $form->create('Item', array('type'=>'file')).PHP_EOL; ?>
	<fieldset>
		<legend><?php __d('lilcart', 'Title and description'); ?></legend>
	<?php
		echo $form->input('id').PHP_EOL;
		echo $form->input('lang', array('type'=>'hidden', 'value'=>@$_lang)).PHP_EOL;
		echo $form->input('title', array('label'=>__d('lilcart', 'Title', true).':')).PHP_EOL;
		echo $form->input('descript', array('label'=>__d('lilcart', 'Description', true).':', 'cols'=>70)).PHP_EOL;
		echo $form->input('image', array('type'=>'file', 'label'=>__d('lilcart', 'Upload image', true).':')).PHP_EOL;
	?>
	</fieldset>
	<fieldset>
		<legend><?php __d('lilcart', 'Prices'); ?></legend>
	<?php
		echo $form->input('price', array('label'=>__d('lilcart', 'Price', true).':')).PHP_EOL;
		echo $form->input('discount', array('label'=>__d('lilcart', 'Discount', true).':')).PHP_EOL;
	?>
	</fieldset>
	<fieldset>
		<legend><?php __d('lilcart', 'Categories'); ?></legend>
	<?php
		echo $form->input('category_id', array(
			'type'=>'select', 
			'label'=>__d('lilcart', 'Select categories', true).':',
			'id' => 'add_category',
			'name'=>'data[Category][Category][]',
			'options'=>$categories, 
			'empty'=>'--'.__d('lilcart', 'select category', true).'--',
			'after'=>' '.$html->link(__d('lilcart', 'Add another', true), 'javascript: void(0);', array('onclick'=>'AddCategory();', 'id'=>'add_category_link')))).PHP_EOL;
		
		$item_categories = Set::filter($html->value('Category.Category'));
		if (!empty($item_categories)) foreach ($item_categories as $category_id) {
			echo $this->element('items/habtm_category_input', array('category_id'=>$category_id, 'category_title'=>$categories[$category_id]));
			echo '<script type="text/javascript">$(\'#remove_category_link_'.$category_id.'\').click(function(){RemoveCategory('.$category_id.');});</script>'.PHP_EOL;
		}
	?>
	</fieldset>
	<?php echo $form->end(__d('lilcart', 'Submit', true));?>
	<script type="text/javascript">
		var categories = new Array();
		<?php
			// put categories into an javascript array
			foreach ($categories as $category_id=>$category_title) {
				echo 'categories['.$category_id.'] = "'.$sanitize->html($category_title).'";'.PHP_EOL;
			}
			
			// disable selected options
			foreach ((array)$html->value('Category.Category') as $category_id) {
				echo '$("#add_category option[@value='.$category_id.']").attr("disabled", true);'.PHP_EOL;
			}
		?>
		
		function AddCategory() {
			var category_id = $('#add_category').val();
			if (category_id) {
				var insert_html = '<?php echo str_replace('\'', '\\\'', str_replace(PHP_EOL, '', $this->element('items/habtm_category_input', array('category_id'=>'js_category_id_placeholder', 'category_title'=>'js_category_title_placeholder')))); ?>';
				
				insert_html = insert_html.replace(/js_category_id_placeholder/g, category_id);
				insert_html = insert_html.replace(/js_category_title_placeholder/g, categories[category_id]);
				$('#add_category_link').after(insert_html);
				
				$('#remove_category_link_'+category_id).click(function(){RemoveCategory(category_id);});
				$('#add_category').val('');
				
				// disable added option
				var o_sel = $('#add_category').get(0);
				for (i=0; i<o_sel.options.length; i++) {
					if (o_sel.options[i].value==category_id) o_sel.options[i].disabled = true;
				}
				//$("#add_category option[@value="+category_id).attr("disabled", true);
			} else {
				alert("<?php __d('lilcart', 'Please select category.'); ?>");
			}
		}
		function RemoveCategory(category_id) {
			//enable added option
			//$("#add_category option[@value="+category_id).attr("disabled", false);
			var o_sel = $('#add_category').get(0);
			for (i=0; i<o_sel.options.length; i++) {
				if (o_sel.options[i].value==category_id) o_sel.options[i].disabled = false;
			}
			$('#category_'+category_id).remove();
		}
	</script>
</div>